import {
  collectionConfigsList,
  collectionConfigsListV3,
  myFruitList,
  collectionConfigsDelete,
  myFruitSale,
  myfruitTop
} from '../../../utils/api/index.js'
Page({
  data: {
    pageNo: 1,
    pageSize: 10,
    fruitList: [],
    show: false,
    userFruitId: '', //用户水果id
    totalNum: 0,
    isAll: false,
  },
  // 
  // onLoad: function (options) {
  //   this.getCompanyList();
  //   this.getData()
  // },
  onShow() {
    this.setData({
      dataList: [],
      pageNo: 1,
      isAll: false,
      pageVisible: false
    })
    this.getData()
  },
  onReachBottom: function () {
    console.log('下拉力')
    //调用刷新时将执行的方法
    if (this.data.pageVisible === false) {
      if (this.data.pageNo * this.data.pageSize < this.data.totalNum) {
        let nowPageNo = this.data.pageNo + 1
        this.setData({
          pageVisible: false,
          pageNo: nowPageNo
        })
        this.getData()
      } else {
        this.setData({
          pageVisible: false
        })
        wx.showToast({
          icon: 'none',
          title: '没有更多了',
        })
      }
    }

  },
  addFruit(e) {
    let configId = e.currentTarget.dataset.configid
    let companyId = e.currentTarget.dataset.companyid
    let fruitId = e.currentTarget.dataset.fruitid

    wx.navigateTo({
      url: `/packageAddress/pages/addCollectionPoint/index?configId=${configId}&companyId=${companyId}&fruitId=${fruitId}`,
    })
  },
  // 删除水果
  deleteFruit(e) {
    const configId = e.currentTarget.dataset.userfruitid
    this.setData({
      configId,
      show: true,
    })
  },
  onClose() {
    this.setData({ show: false });
  },
  onSubmit() {
    collectionConfigsDelete(this.data.configId).then(res => {
      if (res.resultStatus) {
        wx.showToast({
          icon: 'success',
          title: '删除成功',
        })
        this.setData({
          show: false,
        })
        this.data.fruitList = [];
        this.getData()

      }
    })
  },
  // 上下架
  sale(e) {
    console.log(e.currentTarget.dataset, 'dddd');
    const userFruitId = e.currentTarget.dataset.userfruitid
    console.log(userFruitId, 'userFruitId');
    myFruitSale(userFruitId).then(res => {
      if (res.resultStatus) {
        wx.showToast({
          icon: 'success',
          title: '操作成功',
        })
        this.getData()
      }
    })
  },
  // 置顶
  top(e) {
    const userFruitId = e.currentTarget.dataset.userfruitid
    myfruitTop(userFruitId).then(res => {
      if (res.resultStatus) {
        wx.showToast({
          icon: 'success',
          title: '操作成功',
        })
        this.getData()
      }
    })
  },
  // 获取我的水果
  getData() {
    collectionConfigsListV3({
      pageNo: this.data.pageNo,
      pageSize: this.data.pageSize,
    }).then(res => {
      this.setData({
        pageVisible: true
      })
      if (this.data.pageNo > 1) { } else {
        this.setData({
          fruitList: []
        })
      }
      if (res.resultStatus) {

        const data = res.resultData;
        let nowdataList = []
        this.setData({
          totalNum: data.totalNum
        });
        if (data.dataList.length) {
          const fruitList = res.resultData.dataList
          // fruitList.map(item => {
          //   item.price = []
          //   item.attributes.map(item2 => {
          //     // JSON.stringify(item2.attributeCombination)[0].attributeValue
          //     const attributeValue = JSON.parse(item2.attributeCombination)[0].attributeValue
          //     console.log(attributeValue, 'attributeValue');
          //     item.price.push(`${item2.attributeUnitPrice / 100}/${attributeValue}`)
          //   })
          //   return item
          // })
          if (this.data.pageNo > 1) {
            nowdataList = this.data.fruitList.concat(data.dataList);
          } else {
            nowdataList = data.dataList
          }

          this.setData({
            fruitList: nowdataList
          });
        } else {
          this.setData({
            isAll: true
          });
        }
        this.setData({
          pageVisible: false,
        })
      } else {
        this.setData({
          pageVisible: false,
        })
        Toast(res.errorMessage);
      }

    })
  },

})